<template>
  <basic-container>
    <avue-crud
      :option="option"
      :search.sync="search"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @sort-change="sortChange"
      @on-load="onLoad"
    >
    <!-- 
      @row-update="rowUpdate"               编辑数据后确定触发该事件
      @row-save="rowSave"                   新增数据后点击确定触发该事件
      @row-del="rowDel"                     行数据删除时触发该事件
      @search-change="searchChange"         击搜索后触发该事件(由于page分页信息和search搜索信息是sync修饰符，可以直接通过this.page和this.search拿到)
      @search-reset="searchReset"           清空搜索回调方法
      @selection-change="selectionChange"   当选择项发生变化时会触发该事件
      @current-change="currentChange"       分页页码改变时会触发
      @size-change="sizeChange"             pageSize改变时会触发 
      @refresh-change="refreshChange"       点击刷新按钮触发该事件(由于page分页信息和search搜索信息是sync修饰符，可以直接通过this.page和this.search拿到)
      @sort-change="sortChange"             当表格的排序条件发生变化的时候会触发该事件
      @on-load="onLoad"                   
     -->
      <template slot="menuLeft">
        <el-button
          type="danger"
          size="small"
          icon="el-icon-delete"
          plain
          v-if="permission.store_delete"
          @click="handleDelete"
          >删 除
        </el-button>
      </template>
      <template slot-scope="{row,type,size}" slot="menu">
        <el-button icon="el-icon-delete" :size="size" :type="type" v-if="permission.store_delete&&row.isBind == 0" @click="rowDel(row)">删 除</el-button>
      </template>
      <template slot="menuRight">
        <el-button
          type="success"
          size="small"
          plain
          icon="el-icon-upload2"
          v-if="permission.store_import"
          @click="handleImport"
          >导入
        </el-button>
        <el-button
          type="warning"
          size="small"
          plain
          icon="el-icon-download"
          v-if="permission.store_export"
          @click="handleExport"
          >导出
        </el-button>
      </template>
    </avue-crud>
    <el-dialog
      title="数据导入"
      append-to-body
      :visible.sync="excelBox"
      width="555px"
    >
      <avue-form
        :option="excelOption"
        v-model="excelForm"
        :upload-after="uploadAfter"
      >
        <template slot="excelTemplate">
          <el-button type="primary" @click="handleTemplate">
            点击下载<i class="el-icon-download el-icon--right"></i>
          </el-button>
        </template>
      </avue-form>
    </el-dialog>
  </basic-container>
</template>

<script>
import { getList, selectSupList, selectModelList, add, remove, getExport, getExportTemplate, } from "@/api/rb-oem/store/store";
import { mapGetters } from "vuex";
// import { getToken } from "@/util/auth";
import { downloadFileExcel } from "@/util/util";

export default {
  data() {
    return {
      form: {
        rpmRpsNo:""
      },
      query: {},
      search: {},
      loading: true,
      excelBox: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      selectionList: [],
      rptNo:"",
      option: {
        //height: "auto",
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        viewBtn: true,
        selection: true,
        searchMenuSpan: 6,
        columnBtn: false,
        editBtn: false,// 编辑按钮
        delBtn: false,// 编辑按钮
        menuWidth:150,       //操作栏宽度
        selectable:(row)=>{
          return row.isBind == 0;
        },
        // labelWidth:120,      //弹出表单的标题的统一宽度
        // searchLabelWidth: 150, //搜索框标题宽度
        column: [
          {
            label: "类型",
            prop: "rptName",
            type: "input",
            sortable: true,
            addDisplay:false,
            editDisplay:false,
            overHidden: true,
          },
          {
            label: "类型",
            prop: "rptNo",
            type: "select",
            search: true,
            hide:true,
            viewDisplay:false,
            rules: [
              {
                required: true,
                message: "请选择类型",
                trigger: "blur",
              },
            ],
            // cascader: ['rpsNo'],
            filterable:true,
            dicUrl: "/api/rb-oem/oem/store/store/selectTypeList?current=-1&size=-1",
            props: {
              label: 'rptName',
              value: 'rptNo'
            },
            // dicFormatter:(data)=>{
            //   return data.data
            // },
            change: (value) => {
              if (value) {
                // 获取供应商
                this.form.rpsNo = '';
                this.rpmRpsNoList = [];
                this.form.rpmNo = '';
                this.rpmNoList = [];
                this.rptNo=value.value;
                // -----------------------
                this.search.rpsNo = '';
                this.search.rpmNo = '';
                selectSupList(-1,-1,{rptNo:value.value}).then((res)=>{
                  this.rpmRpsNoList = res.data.data;
                  const column = this.findObject(this.option.column, "rpsNo");
                  column.dicData = this.rpmRpsNoList;
                })
              }
            },
            overHidden: true,
          },
          {
            label: "供应商",
            prop: "rpsName",
            type: "input",
            // sortable: true,
            addDisplay:false,
            editDisplay:false,
            overHidden: true,
          },
          {
            label: "供应商",
            prop: "rpsNo",
            type: "select",
            filterable:true,
            // sortable: true,
            search: true,
            hide:true,
            viewDisplay:false,
            rules: [
              {
                required: true,
                message: "请选择供应商",
                trigger: "blur",
              },
            ],
            props: {
              label: 'rpsName',
              value: 'rpsNo'
            },
            // cascader: ['rpmNo'],
            // dicUrl: `/api/rb-oem/oem/store/store/selectSupList?current=-1&size=-1&rptNo={{rptNo}}`,
            change: (value) => {
              if (value) {
                // 获取型号
                this.form.rpmNo = '';
                this.rpmNoList = [];
                // -----------------------
                this.search.rpmNo = '';
                selectModelList(-1,-1,{rpsNo:value.value,rptNo:this.rptNo}).then((res)=>{
                  this.rpmNoList = res.data.data;
                  const column = this.findObject(this.option.column, "rpmNo");
                  column.dicData = this.rpmNoList;
                })
              }
            },
            overHidden: true,
          },
          {
            label: "型号",
            prop: "rpmName",
            type: "input",
            addDisplay:false,
            editDisplay:false,
            overHidden: true,
          },
          {
            label: "型号",
            prop: "rpmNo",
            type: "select",
            search: true,
            hide:true,
            filterable:true,
            viewDisplay:false,
            rules: [
              {
                required: true,
                message: "请选择型号",
                trigger: "blur",
              },
            ],
            props: {
              label: 'rpmName',
              value: 'rpmNo'
            },
            dicData: [],
            overHidden: true,
          },
          {
            label: "产品类型",
            prop: "productType",
            type: "select",
            filterable:true,
            dataType: "number",
            dicUrl: "/api/blade-system/dict/dictionary?code=product_type",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            search: true,
            rules: [
              {
                required: true,
                message: "请选择产品类型",
                trigger: "blur",
              },
            ],
            display:false,
            overHidden: true,
          },
          {
            label: "SN",
            prop: "rsSn",
            type: "input",
            sortable: true,
            search: true,
            rules: [
              {
                required: true,
                message: "请输入SN",
                trigger: "blur",
              },
            ],
            overHidden: true,
          },
          {
            label: "参数",
            prop: "rsParam",
            type: "input",
            search: true,
            rules: [
              {
                required: true,
                message: "请输入参数",
                trigger: "blur",
              },
            ],
            overHidden: true,
          },
          {
            label: "是否绑定",
            prop: "isBind",
            addDisplay:false,
            search: true,
            rules: [
              {
                required: true,
                message: "请选择绑定",
                trigger: "blur",
              },
            ],
            type: "select",
            dataType: "number",
            dicUrl: "/api/blade-system/dict/dictionary?code=yes_or_no",
            props: {
              label: "dictValue",
              value: "dictKey",
            },
            overHidden: true,
          },
          // {
          //   label: "运输单号",
          //   prop: "rsrTransferNo",
          //   type: "input",
          //   sortable: true,
          //   rules: [
          //     {
          //       required: true,
          //       message: "请输入运输单号",
          //       trigger: "blur",
          //     },
          //   ],
          //   hide:true
          // },

        ],
      },
      data: [],
      excelForm: {},
      excelOption: {
        submitBtn: false,
        emptyBtn: false,
        column: [
          {
            label: "模板上传",
            prop: "excelFile",
            type: "upload",
            drag: true,
            loadText: "模板上传中，请稍等",
            span: 24,
            propsHttp: {
              res: "data",
            },
            tip: "请上传 .xls,.xlsx 标准格式文件",
            action: "/api/rb-oem/oem/store/store/import",
          },
          {
            label: "模板下载",
            prop: "excelTemplate",
            formslot: true,
            span: 24,
          },
        ],
      },
      rpmRpsNoList:[],//供应商
      rpmNoList:[],//型号
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.store_add, false),
        viewBtn: this.vaildData(this.permission.store_view, false),
        delBtn: this.vaildData(this.permission.store_delete, false),
        editBtn: this.vaildData(this.permission.store_edit, false),
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach((ele) => {
        ids.push(ele.id);
      });
      return ids.join(",");
    },
  },
  methods: {
    rowSave(row, done, loading) {
      add(row).then(
        () => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          done();
        },
        (error) => {
          loading();
          window.console.log(error);
        }
      );
    },
    rowUpdate() {
      // update(row).then(
      //   () => {
      //     this.onLoad(this.page);
      //     this.$message({
      //       type: "success",
      //       message: "操作成功!",
      //     });
      //     done();
      //   },
      //   (error) => {
      //     loading();
      //     window.console.log(error);
      //   }
      // );
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return remove(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!",
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        // getDetail(this.form.id).then((res) => {
        //   this.form = res.data.data;
        // });
      }
      done();
    },
    // 清空搜索回调方法
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    // 击搜索后触发该事件(由于page分页信息和search搜索信息是sync修饰符，可以直接通过this.page和this.search拿到)
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    // 当选择项发生变化时会触发该事件
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    // 分页页码改变时会触发
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    // pageSize改变时会触发 
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    // 点击刷新按钮触发该事件(由于page分页信息和search搜索信息是sync修饰符，可以直接通过this.page和this.search拿到)
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query)
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
        this.$forceUpdate()
      });
    },
    // 导入
    handleImport() {
      this.excelBox = true;
    },
    // 导出
    handleExport() {
      this.$confirm("是否导出用户数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        getExport(this.search).then((res) => {
            downloadFileExcel(res);
          },
          (error) => {
            window.console.log(error);
          }
        );
      });
    },
    // 点击下载
    handleTemplate() {
      getExportTemplate().then((res) => {
          downloadFileExcel(res);
        },
        (error) => {
          window.console.log(error);
        }
      );
    },
    uploadAfter(res, done) {
      this.excelBox = false;
      this.refreshChange();
      done();
    },
    // 当表格的排序条件发生变化的时候会触发该事件
    sortChange(val) {
      if (val.order == "ascending") {
        //正序
        this.query.ascs = val.prop;
        this.query.descs = "";
      } else {
        //倒序
        this.query.descs = val.prop;
        this.query.ascs = "";
      }
      this.refreshChange();
    },
  },
};
</script>


